package com.example.demo.controller;

import com.mongodb.client.MongoCollection;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/CollectionController")
@Api(tags = "集合管理")
public class CollectionController {
    @Autowired
    private MongoTemplate mongoTemplate;

    @PostMapping("/insertCollection")
    @ApiOperation(value = "insert",notes = "添加集合")
    public String insertCollectionByName(
            String collectionName
    ){
        boolean b = mongoTemplate.collectionExists(collectionName);
        if(!b){
            MongoCollection<Document> collection = mongoTemplate.createCollection(collectionName);
            return "创建成功";
        }else {
            return "集合已经存在";
        }
    }

    @DeleteMapping("deleteCollectionByName")
    @ApiOperation(value = "delete",notes = "删除集合")
    public String deleteCollectionByName(
            String collectionName
    ){
        mongoTemplate.dropCollection(collectionName);
        return "删除成功";
    }
}
